package com.cy.ppj.web.admin.order;

import com.cy.ppj.comm.constant.Constants;
import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.ao.order.MallOrderAuditPurchaseProofAO;
import com.cy.ppj.model.dto.manager.ManagerLoginDTO;
import com.cy.ppj.model.enums.MessageType;
import com.cy.ppj.model.query.order.MallOrderListAdminQuery;
import com.cy.ppj.model.vo.order.MallOrderDetailAdminVO;
import com.cy.ppj.model.vo.order.MallOrderListAdminVO;
import com.cy.ppj.service.message.MessageService;
import com.cy.ppj.service.order.MallOrderService;
import com.cy.ppj.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 后台-马先生商城家具分期订单管理
 * @Author tanyilin
 * @Date 2019/4/3 11:38
 */
@Slf4j
@RestController
@RequestMapping("/admin/mall/order")
@Api(tags = "后台-分期订单管理")
public class AdminMallOrderController extends BaseController {

    @Autowired
    private MallOrderService mallOrderService;
    @Autowired
    private MessageService messageService;

    @ApiOperation("申请中订单")
    @GetMapping("/applying/list")
    public PageBean<MallOrderListAdminVO> selectApplyingMallOrderList(MallOrderListAdminQuery query) {

        PageBean<MallOrderListAdminVO> pageBean = mallOrderService.selectApplyingMallOrderList(query);
        return pageBean;
    }

    @ApiOperation("已授信订单")
    @GetMapping("/decision/pass/list")
    public PageBean<MallOrderListAdminVO> selectDecisionPassMallOrderList(MallOrderListAdminQuery query) {

        PageBean<MallOrderListAdminVO> pageBean = mallOrderService.selectDecisionPassMallOrderList(query);
        return pageBean;
    }

    @ApiOperation("待接单订单")
    @GetMapping("/unassigned/list")
    public PageBean<MallOrderListAdminVO> selectUnassignedMallOrderList(MallOrderListAdminQuery query) {

        PageBean<MallOrderListAdminVO> pageBean = mallOrderService.selectUnassignedMallOrderList(query);
        return pageBean;
    }

    @ApiOperation("审核中订单")
    @GetMapping("/auditing/list")
    public PageBean<MallOrderListAdminVO> selectAuditingMallOrderList(MallOrderListAdminQuery query) {

        // 当前登录的管理员账户
        ManagerLoginDTO manager = this.getManager();
        if(Constants.AUDIT_ROLE_ID.equals(manager.getRoleId())) {
            // 如果当前管理员是审核人员角色，那就只能查看他所接的单
            query.setManagerId(manager.getManagerId());
        }

        PageBean<MallOrderListAdminVO> pageBean = mallOrderService.selectAuditingMallOrderList(query);
        return pageBean;
    }

    @ApiOperation("已审核订单")
    @GetMapping("/audited/list")
    public PageBean<MallOrderListAdminVO> selectAuditedMallOrderList(MallOrderListAdminQuery query) {

        // 当前登录的管理员账户
        ManagerLoginDTO manager = this.getManager();
        if(Constants.AUDIT_ROLE_ID.equals(manager.getRoleId())) {
            // 如果当前管理员是审核人员角色，那就只能查看他所接的单
            query.setManagerId(manager.getManagerId());
        }

        PageBean<MallOrderListAdminVO> pageBean = mallOrderService.selectAuditedMallOrderList(query);
        return pageBean;
    }

    @ApiOperation("申请拒绝订单")
    @GetMapping("/reject/list")
    public PageBean<MallOrderListAdminVO> selectRejectMallOrderList(MallOrderListAdminQuery query) {

        // 当前登录的管理员账户
//        ManagerLoginDTO manager = this.getManager();
//        if(Constants.AUDIT_ROLE_ID.equals(manager.getRoleId())) {
//            // 如果当前管理员是审核人员角色，那就只能查看他所接的单
//            query.setManagerId(manager.getManagerId());
//        }

        PageBean<MallOrderListAdminVO> pageBean = mallOrderService.selectRejectMallOrderList(query);
        return pageBean;
    }

    @ApiOperation("分期订单详情")
    @GetMapping("/detail")
    public Result<MallOrderDetailAdminVO> detail(@RequestParam(value = "orderId") Long orderId) {
        MallOrderDetailAdminVO mallOrderDetailAdminVO = mallOrderService.selectMallOrderDetailAdminVO(orderId);
        return ok("获取分期订单详情成功", mallOrderDetailAdminVO);
    }

    @ApiOperation("接单")
    @PostMapping("/receive")
    public Result receive(@RequestParam(value = "orderId") Long orderId) {

        mallOrderService.assignManager(orderId, this.getManager().getManagerId());

        return ok();
    }

    @ApiOperation("审核购买证明")
    @PostMapping("/do/audit/purchaseProof")
    public Result doAuditPurchaseProof(@Validated MallOrderAuditPurchaseProofAO auditAO, BindingResult bindingResult) {

        this.checkError(bindingResult);

        mallOrderService.doAuditPurchaseProof(auditAO, this.getManager().getManagerId());

        if("PASS".equals(auditAO.getAuditResult())) {
            //发送微信通知：马先生后台订单审核通过
            messageService.sendMessage(String.valueOf(auditAO.getOrderId()), MessageType.ORDER_BANKMA_AUDIT_PASS);

        }else if("REJECT".equals(auditAO.getAuditResult())) {
            //发送微信通知：马先生后台订单审核拒绝
            messageService.sendMessage(String.valueOf(auditAO.getOrderId()), MessageType.ORDER_BANKMA_AUDIT_REJECT);
        }

        return ok();
    }

}
